package com.qzsoft.system.modules.sysstation.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qzsoft.system.modules.sysstation.dto.SysStationListDto;
import com.qzsoft.system.modules.sysstation.entity.SysStationEntity;
import com.qzsoft.system.modules.sysstation.enums.SysStationType;
import com.qzsoft.system.modules.sysstation.mapper.SysStationMapper;
import com.qzsoft.system.modules.sysstation.service.SysStationAsphaltMaterialService;
import com.qzsoft.system.modules.sysstation.service.SysStationAsphaltSensorService;
import com.qzsoft.system.modules.sysstation.service.SysStationService;
import com.qzsoft.system.modules.zhjdsysdept.entity.ZhjdSysDeptEntity;
import com.qzsoft.system.modules.zhjdsysdept.service.ZhjdSysDeptService;
import com.qzsoft.zhjd.common.annotation.ZhjdDataFilter;
import com.qzsoft.zhjd.common.components.BaseServiceComponent;
import com.qzsoft.zhjd.common.enums.BizCode;
import com.qzsoft.zhjd.common.utils.RenExceptionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * @author linhuawei
 * @ClassName SysStationServiceImpl
 * @Description 拌合楼业务接口实现
 * @since 2021-12-06
 */
@Service
public class SysStationServiceImpl extends ServiceImpl<SysStationMapper, SysStationEntity>
        implements SysStationService {

    @Autowired
    private BaseServiceComponent baseServiceComponent;

    @Autowired
    private SysStationAsphaltSensorService sysStationAsphaltSensorService;

    @Autowired
    private SysStationAsphaltMaterialService sysStationAsphaltMaterialService;

    @Autowired
    private ZhjdSysDeptService zhjdSysDeptService;

    @Override
    @ZhjdDataFilter(tableAlias = "t1")
    public IPage<SysStationListDto> pageDto(Map<String, Object> params) {
        IPage<SysStationListDto> page = baseServiceComponent.getPage(params, SysStationListDto.class);
        return baseMapper.queryDto(page, params);
    }

    @Override
    @ZhjdDataFilter
    public List<SysStationEntity> list(Map<String, Object> params) {
        QueryWrapper<SysStationEntity> queryWrapper = new QueryWrapper<SysStationEntity>();
        if (ObjectUtils.isNotEmpty(params.get("type"))) {
            queryWrapper.eq("type", params.get("type"));
        }
        if (ObjectUtils.isNotEmpty(params.get("name"))) {
            queryWrapper.like("name", params.get("name"));
        }
        if (ObjectUtils.isNotEmpty(params.get("baseCode"))) {
            queryWrapper.like("base_code", params.get("baseCode"));
        }
        if (ObjectUtils.isNotEmpty(params.get(ZhjdDataFilter.DATA_FILTER_SQL))) {
            queryWrapper.apply(params.get(ZhjdDataFilter.DATA_FILTER_SQL).toString());
        }
//        if(ObjectUtils.isNotEmpty(params.get(ZhjdDataFilter.DATA_FILTER_PARAMS))) {
//            List<String> baseCodes = (List<String>) params.get(ZhjdDataFilter.DATA_FILTER_PARAMS);
//            queryWrapper.in("base_code", baseCodes);
//        }
        queryWrapper.orderByAsc("code");
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public boolean save(SysStationEntity entity) {
        // 获取工地信息
        String baseCode = entity.getBaseCode();
        ZhjdSysDeptEntity base = zhjdSysDeptService.getBaseByCode(baseCode);
        if (null == base) {
            RenExceptionUtils.throwEx(BizCode.PARAMS_ERROR, "所属工地不存在");
        }

        // 判断拌和站类型
        String type = entity.getType();
        SysStationType stationType = SysStationType.getByValue(type);
        if (null == stationType) {
            RenExceptionUtils.throwEx(BizCode.PARAMS_ERROR, "未知的拌和站类型[" + type + "]");
        }

        // 设置拌合楼名称、编号
        String codeSuffix = entity.getCode();
        String name = base.getName() + "-" + stationType.getLabel() + "#" + codeSuffix;
//        String code = baseCode + stationType.getCode() + codeSuffix;

        // 判断编号是否重复
        QueryWrapper<SysStationEntity> queryWrapper = new QueryWrapper<SysStationEntity>();
        queryWrapper.eq("base_code", entity.getBaseCode());
        queryWrapper.eq("type", entity.getType());
        queryWrapper.eq("code", entity.getCode());
        Integer count = baseMapper.selectCount(queryWrapper);
        if (null != count && 0 < count) {
            RenExceptionUtils.throwEx(BizCode.PARAMS_ERROR, "拌和站编号[" + entity.getCode() + "]重复");
        }

        // 执行添加操作
        entity.setName(name);
//        entity.setCode(code);
        return super.save(entity);
    }

    @Override
    public void updateSensorConfig(String id, Integer state) {
        // 更新传感器校正状态
        UpdateWrapper<SysStationEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id);
        updateWrapper.set("sensor_config", 1);
        baseMapper.update(null, updateWrapper);
    }

    @Override
    public void updateMaterialConfig(String id, Integer state) {
        // 更新物料材料状态
        UpdateWrapper<SysStationEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id);
        updateWrapper.set("material_config", 1);
        baseMapper.update(null, updateWrapper);
    }

    @Override
    public SysStationEntity getStationByCode(String stationCode) {
        return baseMapper.getStationByCode(stationCode);
    }

    @Override
    public SysStationEntity getByTypeAndCode(SysStationType type, String stationCode) {
        QueryWrapper<SysStationEntity> queryWrapper = new QueryWrapper<SysStationEntity>();
        queryWrapper.eq("code", stationCode);
        queryWrapper.eq("type", type.getValue());
        queryWrapper.last("LIMIT 0,1");
        return baseMapper.selectOne(queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeById(Serializable id) {
        // 根据拌合楼删除传感器信息
        sysStationAsphaltSensorService.deleteByStationId(id);

        // 根据拌合楼删除物料信息
        sysStationAsphaltMaterialService.deleteByStationId(id);

        // TODO 水稳处理

        // 删除拌合楼信息
        return super.removeById(id);
    }

}
